package com.example.myapp;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;

public class InputStateActivity extends AppCompatActivity {
    private static final String TAG ="InputStateActivity";
    EditText weight,age,height;
    RadioGroup sex,type;
    RadioButton male,female,never,min,mid,max;
    Button input_btn;
    String str,str_type;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_state);

        weight=findViewById(R.id.weight_edit);
        height=findViewById(R.id.height_edit);
        age = findViewById(R.id.age_edit);
        sex = findViewById(R.id.sex_radio);
        male = findViewById(R.id.male);
        female = findViewById(R.id.female);
        //获得选中的值

        type = findViewById(R.id.type_radio);
        never = findViewById(R.id.never);
        min = findViewById(R.id.min);
        mid = findViewById(R.id.mid);
        max = findViewById(R.id.max);



        //button触发事件（保存到数据库）
        input_btn = findViewById(R.id.input_btn);
        input_btn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
              //  Log.i(TAG, "onClick: male=" + male.getText() + male.isChecked());
              //  Log.i(TAG, "onClick: female=" + female.getText() + female.isChecked());
                if(female.isChecked()==true){
                    str = female.getText().toString();
                }
                else if (male.isChecked()==true){
                    str=male.getText().toString();
                }
                if (max.isChecked()==true){
                    str_type = max.getText().toString();
                }
                else if (min.isChecked()==true){
                    str_type = min.getText().toString();
                }
                else if (mid.isChecked()==true){
                    str_type = mid.getText().toString();
                }
                else {
                    str_type = never.getText().toString();
                }
                MyDatabaseHelper myDB = new MyDatabaseHelper(InputStateActivity.this);
                myDB.addState(Integer.valueOf(age.getText().toString().trim()),
                        Integer.valueOf(height.getText().toString().trim()),
                        Integer.valueOf(weight.getText().toString().trim()),
                        str,
                        str_type
                );

                SharedPreferences sharedPreferences = getSharedPreferences("mystate", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("myweight",weight.getText().toString());
                editor.putString("myheight",height.getText().toString());
                editor.putString("myage",age.getText().toString());
                editor.putString("mysex",str);
                editor.putString("mytype",str_type);
                editor.commit();
                Log.i(TAG,"onCreate:数据以保存");

            }


        });





    }







}
